package com.filldream.fastboot.web.controller.pc;

import com.filldream.fastboot.base.entity.SysServerInfo;
import com.filldream.fastboot.base.request.IdListSTO;
import com.filldream.fastboot.base.request.IdSTO;
import com.filldream.fastboot.base.request.ModifyStatusSTO;
import com.filldream.fastboot.base.request.pc.serverInfo.AddServerInfoSTO;
import com.filldream.fastboot.base.request.pc.serverInfo.ModifyServerInfoSTO;
import com.filldream.fastboot.base.request.pc.serverInfo.ServerInfoPageSTO;
import com.filldream.fastboot.common.annotation.Log;
import com.filldream.fastboot.common.annotation.PassTokenAnnotation;
import com.filldream.fastboot.common.annotation.RolePermissions;
import com.filldream.fastboot.common.entity.B;
import com.filldream.fastboot.common.entity.P;
import com.filldream.fastboot.common.entity.R;
import com.filldream.fastboot.common.util.FileIOUtil;
import com.filldream.fastboot.common.util.RandomUtil;
import com.filldream.fastboot.common.util.ShellUtil;
import com.filldream.fastboot.common.util.StringUtil;
import com.filldream.fastboot.core.service.SysServerInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

@RestController
@RequiredArgsConstructor
@Api(tags = "PC-服务器管理")
@RequestMapping("/pc/serverInfo")
@PassTokenAnnotation
public class PcSysServerInfoController {

    private final SysServerInfoService sysServerInfoService;

    @Log(value = "服务器列表")
    @ApiOperation(value = "服务器列表")
    @GetMapping("/page")
    @RolePermissions("serverInfo:page")
    public P<SysServerInfo> page(ServerInfoPageSTO serverInfoPageSTO){
        return sysServerInfoService.queryPage(serverInfoPageSTO);
    }

    @Log(value = "添加服务器")
    @ApiOperation(value = "添加服务器")
    @PostMapping("/add")
    @RolePermissions("serverInfo:add")
    public B add(@RequestBody @Valid AddServerInfoSTO addServerInfoSTO){
        return sysServerInfoService.add(addServerInfoSTO);
    }

    @Log(value = "修改服务器")
    @ApiOperation(value = "添加服务器")
    @PostMapping("/modify")
    @RolePermissions("serverInfo:modify")
    public B modify(@RequestBody @Valid ModifyServerInfoSTO modifyServerInfoSTO){
        return sysServerInfoService.modify(modifyServerInfoSTO);
    }

    @Log(value = "删除服务器")
    @ApiOperation(value = "删除服务器")
    @PostMapping("/del")
    @RolePermissions("serverInfo:del")
    public B del(@RequestBody @Valid IdListSTO idListSTO){
        return sysServerInfoService.del(idListSTO.getIds());
    }

    @Log(value = "服务器详情")
    @ApiOperation(value = "服务器详情")
    @GetMapping("/get")
    @RolePermissions("serverInfo:get")
    public R<SysServerInfo> get(IdSTO idSTO){
        return R.successd(sysServerInfoService.getById(idSTO.getId()));
    }

    @Log(value = "服务器简要详情")
    @ApiOperation(value = "服务器简要详情")
    @GetMapping("/simpleGet")
    @RolePermissions("serverInfo:simpleGet")
    public R<SysServerInfo> simpleGet(IdSTO idSTO){
        SysServerInfo serverInfo = sysServerInfoService.getById(idSTO.getId());
        if( serverInfo != null ){
            serverInfo.setServerPassword("***");
            serverInfo.setCloudPassword("***");
            serverInfo.setServerUsername(StringUtil.hideName(serverInfo.getServerUsername()));
            serverInfo.setCloudUsername(StringUtil.hideName(serverInfo.getCloudUsername()));
            serverInfo.setServerKey(StringUtil.hideName(serverInfo.getServerKey()));
            serverInfo.setServerSecret(StringUtil.hideName(serverInfo.getServerSecret()));
        }
        return R.successd(serverInfo);
    }

    @Log(value = "修改服务器状态")
    @ApiOperation(value = "修改服务器状态")
    @PostMapping("/modifyStatus")
    @RolePermissions("serverInfo:modifyStatus")
    public B modifyStatus(@RequestBody @Valid ModifyStatusSTO modifyStatusSTO){
        SysServerInfo sto = new SysServerInfo();
        sto.setId( modifyStatusSTO.getId() );
        sto.setStatus( modifyStatusSTO.getStatus() );
        return B.checkSuccess(sysServerInfoService.updateById( sto ));
    }

    @Log(value = "上传证书")
    @ApiOperation(value = "上传证书")
    @PostMapping("/uploadPem")
    @RolePermissions("serverInfo:uploadPem")
    public R<List<String>> uploadPem(@RequestParam(value = "file", required = false) MultipartFile file){
        String suffix = FileIOUtil.getFileSuffix( file.getOriginalFilename() );
        String s = FileIOUtil.saveFileToLocal(file,  RandomUtil.randAlphaNumString(9)  + suffix);
        return R.successd(Arrays.asList(s));
    }


    @Log(value = "连接是否正确")
    @ApiOperation(value = "连接是否正确")
    @GetMapping("/check")
    @RolePermissions("serverInfo:check")
    public R<List<String>> check(IdSTO idSTO){
        SysServerInfo byId = sysServerInfoService.getById(idSTO.getId());
        ShellUtil shellUtil = new ShellUtil(byId.getServerHost(),byId.getServerUsername(), byId.getServerPassword(),  byId.getServerPort());
        String testCmd = "echo '  Success login to " + byId.getServerHost() + "' && ";
        testCmd += "echo $(whereis wget) && echo $(whereis zip) && echo $(whereis rar) && echo $(java -version)";
        List<String> resultList = shellUtil.executeForResultList(testCmd);
        resultList.add("---------------服务器 " + byId.getServerHost() + " 连接" + (ObjectUtils.isNotEmpty(resultList) ? "成功" : "失败或超时") + "---------------");
        return R.autoSuccess(resultList);
    }

}